package com.tensai.study.java.basic.asynchronous.thread.executor.task;

import org.junit.Test;

/**
 * {@link Runnable}接口是线程辅助类，仅定义了一个方法run()方法<br />
 * 实现多线程<br />
 * {@link Runnable#run()}方法可以被多个线程共享，适用于多个进程处理一种资源的问题
 *
 * @author Tensai
 * @see java.lang.FunctionalInterface
 * @since java-1.0
 */
public class RunnableTest {

	/**
	 * 实现Runnable接口，并不能启动或者说实现一个线程。
	 * {@link Runnable}接口，并不能代表一个线程。
	 * {@link Runnable}接口和线程是两个不同的概念！
	 */
	@Test
	public void test() {
		MyRunnable myRunnable1 = new MyRunnable("Runnable1");
		MyRunnable myRunnable2 = new MyRunnable("Runnable2");
		MyRunnable myRunnable3 = new MyRunnable("Runnable3");

		Thread myThread1 = new Thread(myRunnable1);
		myThread1.start();
		System.out.println("myThread1 id =" + myThread1.getId());
		Thread myThread2 = new Thread(myRunnable2);
		myThread2.start();
		System.out.println("myThread1 id =" + myThread2.getId());
		Thread myThread3 = new Thread(myRunnable3);
		myThread3.start();
		System.out.println("myThread1 id =" + myThread3.getId());

	}


	@Test
	public void compareThreadTest() {
		MyThread myThread1 = new MyThread("线程1");
		MyThread myThread2 = new MyThread("线程2");
		MyThread myThread3 = new MyThread("线程3");

		myThread1.start();
		myThread2.start();
		myThread3.start();

		System.out.println("myThread1 id =" + myThread1.getId());
		System.out.println("myThread1 id =" + myThread2.getId());
		System.out.println("myThread1 id =" + myThread3.getId());


	}

	public static class MyThread extends Thread {

		private final String name;

		public MyThread(String name) {
			this.name = name;
		}

		@Override
		public void run() {
			System.out.println("MyThread is " + name);
		}
	}

	public static class MyRunnable implements Runnable {

		private final String name;

		public MyRunnable(String name) {
			this.name = name;
		}

		@Override
		public void run() {
			System.out.println("MyRunnable is " + name);
		}
	}
}
